﻿using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

public class ECSCubeCreator : MonoBehaviour {
    public int count;
    public float radius;

    public MeshInstanceRenderer render;

    /// <summary>
    /// 使用ecs创建连续内存的物体，要用这个manager去创建
    /// </summary>
    EntityManager manager;

    /// <summary>
    /// 需要连续存放在内存中的类型
    /// </summary>
    EntityArchetype atype;

    // Use this for initialization
    void Start() {
        PlaceRandomCubes(count, radius);
    }

    public void PlaceRandomCubes(int count, float radius) {

        manager = World.Active.GetOrCreateManager<EntityManager>();

        atype = manager.CreateArchetype();

        for (int i = 0; i < count; i++) {
            //创建实体(纯ECS模式)
            Entity item = manager.CreateEntity(atype);

            manager.AddComponentData(item, new Position() { Value = Random.insideUnitSphere * radius });
            manager.AddComponentData(item, new ECS2Data() { time = Time.deltaTime });

            //manager.AddComponentData(item, new LocalToWorld());
            //manager.AddComponentData(item, new WorldMeshRenderBounds());

            manager.AddSharedComponentData(item, new MeshInstanceRenderer() {
                mesh = render.mesh,
                material = render.material,
                subMesh = render.subMesh,
                castShadows = render.castShadows,
                receiveShadows=render.receiveShadows
            });
        }
    }
}
